app.controller('groupController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams','GridFactory','uiGridConstants',
        function($scope, httpWrapService, dialogs, $state, $stateParams,GridFactory,uiGridConstants) {

    var columnDefs = [
        {name: "机构名称", cellTemplate: '<div class="ui-grid-cell-contents" title="TOOLTIP"><span ng-repeat="i in row.entity.$$treeLevel | range">&nbsp;&nbsp;&nbsp;&nbsp;</span>{{row.entity.name}}</div>'},
        {name: "编码", field: "group",maxWidth: 120} ,
        {name: "机构等级", field: "type",maxWidth: 90,
            cellFilter: 'dictionaryFiter : "sysOfficeGrade"',
            filter : {
                noTerm: true,
                type: uiGridConstants.filter.SELECT,
                selectOptions : $scope.dictionary.sysOfficeGrade
            }} ,
        {name: "区域", field: "areaId", cellFilter: 'sys_region_Fiter : "name"'} ,
        {name: "邮箱", field: "email"} ,
        {name: "负责人", field: "master",maxWidth: 90} ,
        {name: "电话", field: "phone"} ,
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-default m-b-xs w-xs" ng-click="grid.appScope.view(this)">查看/编辑</button>'
        },
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-danger m-b-xs w-xs" ng-disabled="grid.appScope.Grid.deletes[\'{{COL_FIELD}}\']" ng-click="deleteBtnDisable[id] = true; grid.appScope.delete(this);">删除</button>'
        }
    ];


    $scope.gridOptions  = {
        columnDefs: columnDefs,
        enableColumnResizing: true,
        enablePaginationControls : false,
        enableFiltering: true,
        enableFullRowSelection: true,
        showTreeExpandNoChildren: false,
        multiSelect : false,
        data : [],
        onRegisterApi : function(gridApi){
            $scope.gridApi = gridApi;
        }
    }

    httpWrapService.post("system/group/list.do", {}).then(function(res){
        $scope.gridOptions.data = res.data.data;
        setTimeout(function(){
            $scope.gridApi.treeBase.expandAllRows();
        }, 300)
    });


    $scope.add = function() {
        var rows = $scope.gridApi.selection.getSelectedRows();
        if(rows.length > 1) {
            dialogs.error('错误', '不能多选，请选择一个组织机构后增加', {size: 'md'});
            return ;
        }
        var parentId;
        if(rows[0]){
            parentId = rows[0].id;
        }
        $state.go('app.system.group.view', {type : "add", parentId : parentId});
    };

    $scope.view = function(target) {
        $state.go('app.system.group.view', {id : target.$parent.row.entity.id, type:"update" });
    };

    $scope.delete = function(target) {
        var id = target.$parent.row.entity.id;
        httpWrapService.defaultDelete('system/group/delete/'+ id +'.do').then(function(res){
            var index = $scope.gridOptions.data.indexOf(target.$parent.row.entity);
            $scope.gridOptions.data.splice(index, 1);
        });
    };

}]);

app.filter('range', function(){
        return function(n) {
            var res = [];
            for (var i = 0; i < n; i++) {
                res.push(i);
            }
            return res;
        };
    });